//
//  NSDate+Category.m
//  YLB
//
//  Created by Zoo Chow on 19/02/2017.
//  Copyright © 2017 Zoo. All rights reserved.
//

#import "NSDate+Category.h"

@implementation NSDate (Category)

+ (NSDate*)convertDateByString:(NSString*)dateString {
    if (dateString) {
        NSArray * tarr = [dateString componentsSeparatedByString:@"."];
        dateString = [tarr firstObject];
        dateString = [dateString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
//        dateString = [dateString stringByReplacingOccurrencesOfString:@"Z" withString:@""];
        [dateString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
        NSString * format = @"yyyy-MM-dd HH:mm:ss", * noHoursFormat = @"yyyy-MM-dd", * noYearFormat = @"MM-dd HH:mm:ss",
                 * tzFormat = @"yyyy-MM-dd'T'HH:mm:ssZ", * noSecFormat = @"yyyy-MM-dd HH:mm", * sFormat = @"MM/dd/yyyy HH:mm:ss",
                 * ysFormat = @"yyyy/MM/dd HH:mm:ss";
        if ([dateString length]==[noHoursFormat length]) {
            format = noHoursFormat;
        } else if ([dateString length]==[noYearFormat length]) {
            format = noYearFormat;
        } else if ([dateString length]==[noSecFormat length]) {
            format = noSecFormat;
        } else {
            NSRange range = [dateString rangeOfString:@"+"];
            if (range.location != NSNotFound) {
                format = tzFormat;
            } else {
                range = [dateString rangeOfString:@"/"];
                if (range.location == 2) {
                    format = sFormat;
                } else if (range.location == 4) {
                    format = ysFormat;
                }
            }
        }
        [dateFormatter setDateFormat:format];
        NSDate * date = [dateFormatter dateFromString:dateString];
        return date;
    }
    return nil;
}

+ (NSString*)convertDateToDateString:(NSDate *)date {
    if (date) {
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        return [dateFormatter stringFromDate:date];
    }
    return @"";
}

+ (NSString *)convertDateToDayOfDateString:(NSDate *)date {
    if (date) {
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        return [dateFormatter stringFromDate:date];
    }
    return @"";
}

- (NSString *)convertToHour {
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"HH:mm"];
    return [dateFormatter stringFromDate:self];
}

+ (NSString*)convertDateToDescription:(NSDate *)srcDate {
    if (srcDate==nil) {
        return @"";
    }
    NSString * retstr = nil;
    NSDateComponents * curcomp = [[self class] convertDateToComponents:srcDate];
    NSDateComponents * nowcomp = [[self class] convertDateToComponents:[NSDate date]];
    if (curcomp.year != nowcomp.year) {
        retstr = [[[[self class] convertDateToDateString:srcDate] componentsSeparatedByString:@" "] firstObject];
    } else {
        if (curcomp.month == nowcomp.month && curcomp.weekOfMonth == nowcomp.weekOfMonth && curcomp.day == nowcomp.day) {
            retstr = [NSString stringWithFormat:@"今天 %02ld:%02ld", (long)curcomp.hour,(long)curcomp.minute];
        } else {
            if (curcomp.month == nowcomp.month && curcomp.weekOfMonth == nowcomp.weekOfMonth) {
                NSInteger dayCount = nowcomp.day-curcomp.day;
                if (dayCount<=2 && dayCount>0) {
                    retstr = [NSString stringWithFormat:@"%@ %02ld:%02ld", [@[@"昨天",@"前天"]objectAtIndex:dayCount-1], (long)curcomp.hour,(long)curcomp.minute];
                } else if (dayCount < 0 && dayCount>=-2) {
                    retstr = [NSString stringWithFormat:@"%@ %02ld:%02ld", [@[@"明天",@"后天"]objectAtIndex:labs(dayCount)-1], (long)curcomp.hour,(long)curcomp.minute];
                } else {
                    retstr = [NSString stringWithFormat:@"%ld天%@", (long)labs(dayCount), dayCount>0?@"前":@"后"];
                }
            } else {
                if (curcomp.month == nowcomp.month) {
                    NSInteger dayCount = nowcomp.day-curcomp.day;
                    if (dayCount<=2 && dayCount>0) {
                        retstr = [NSString stringWithFormat:@"%@ %02ld:%02ld", [@[@"昨天",@"前天"]objectAtIndex:dayCount-1], (long)curcomp.hour,(long)curcomp.minute];
                    } else if (dayCount < 0 && dayCount>=-2) {
                        retstr = [NSString stringWithFormat:@"%@ %02ld:%02ld", [@[@"明天",@"后天"]objectAtIndex:labs(dayCount)-1], (long)curcomp.hour,(long)curcomp.minute];
                    } else {
                        NSInteger weekCount = (nowcomp.weekOfMonth-curcomp.weekOfMonth);
                        if (weekCount > 0) {
                            retstr = [NSString stringWithFormat:@"%ld周前", (long)labs(weekCount)];
                        } else {
                            retstr = [[self class] convertDateToDateString:srcDate];
                        }
                    }
                } else {
                    NSInteger monthCount = (nowcomp.month-curcomp.month);
                    if (monthCount > 0) {
                        retstr = [NSString stringWithFormat:@"%ld月前", (long)labs(monthCount)];
                    } else {
                        retstr = [[self class] convertDateToDateString:srcDate];
                    }
                }
            }
        }
    }
    return retstr;
}

+ (NSString*)convertDateStringToDescription:(NSString *)dateString {
    return [[self class] convertDateToDescription:[[self class] convertDateByString:dateString]];
}

+ (NSDateComponents*)convertDateToComponents:(NSDate*)date {
    if (date) {
        NSCalendarUnit units = NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitMonth|NSCalendarUnitSecond|NSCalendarUnitYear|NSCalendarUnitWeekOfMonth;
        NSDateComponents * components = [[NSCalendar currentCalendar] components:units fromDate:date];
        return components;
    } else {
        return nil;
    }
}

+ (NSString *)countDownFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSInteger interval = [toDate timeIntervalSinceDate:fromDate];
    NSInteger sec_per_hour = 3600, hour = interval/sec_per_hour, sec_per_min = 60;
    interval = interval%sec_per_hour;
    NSInteger minute = interval/sec_per_min, second = interval%sec_per_min;
    if (hour) {
        return [NSString stringWithFormat:@"%02ld:%02ld:%02ld", (long)hour, (long)minute, (long)second];
    } else {
        return [NSString stringWithFormat:@"%02ld:%02ld", (long)minute, (long)second];
    }
}

+ (NSDate *)getNextHour:(NSDate *)date {
    NSDateComponents * comp = [NSDate convertDateToComponents:date];
    NSInteger hour = comp.hour+1, year = comp.year, month = comp.month, day = comp.day;
    [comp setValue:0 forComponent:NSCalendarUnitMinute];
    [comp setValue:0 forComponent:NSCalendarUnitSecond];
    if (hour>=24) {
        hour = 0;
        day++;
        if (day>[NSDate daysOfMonth:month]) {
            month++;
            if (month>=12) {
                month = 0;
                year++;
            }
        }
    }
    [comp setValue:hour forComponent:NSCalendarUnitHour];
    [comp setValue:day forComponent:NSCalendarUnitDay];
    [comp setValue:month forComponent:NSCalendarUnitMonth];
    [comp setValue:year forComponent:NSCalendarUnitYear];
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar dateFromComponents:comp];
}

+ (NSDate *)getNextHourBefore20Minute:(NSDate *)date {
    NSDateComponents * comp = [NSDate convertDateToComponents:date];
    NSInteger hour = comp.hour+1, year = comp.year, month = comp.month, day = comp.day, minute = comp.minute;
    [comp setValue:0 forComponent:NSCalendarUnitSecond];
    if (minute>=40) {
        minute = 30;
        if (minute>=60) {
            minute = 0;
            hour++;
        }
    } else {
        minute = 0;
    }
    if (hour>=24) {
        hour = 0;
        day++;
        if (day>[NSDate daysOfMonth:month]) {
            month++;
            if (month>=12) {
                month = 0;
                year++;
            }
        }
    }
    [comp setValue:minute forComponent:NSCalendarUnitMinute];
    [comp setValue:hour forComponent:NSCalendarUnitHour];
    [comp setValue:day forComponent:NSCalendarUnitDay];
    [comp setValue:month forComponent:NSCalendarUnitMonth];
    [comp setValue:year forComponent:NSCalendarUnitYear];
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar dateFromComponents:comp];
}

+ (NSInteger)daysOfMonth:(NSInteger)month {
    NSDateComponents * comp = [NSDate convertDateToComponents:[NSDate date]];
    [comp setValue:month forComponent:NSCalendarUnitMonth];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[comp date]];
    return range.length;
}

+ (NSString *)calculateHourAndMinutesInMinutes:(NSInteger)minutes {
    NSString * title = @"", *suffix = @"分钟", *prefix = @"小时";
    if (minutes < 60) {
        title = [NSString stringWithFormat:@"%ld %@", (long)minutes, suffix];
    } else {
        NSInteger hour = minutes/60, last = minutes%60;
        if (last) {
            title = [NSString stringWithFormat:@"%ld%@%ld%@", (long)hour, prefix, (long)last, suffix];
        } else {
            title = [NSString stringWithFormat:@"%ld %@", (long)hour, prefix];
        }
    }
    return title;
}

- (NSDate *)convertGreenwichMeanTime {
    NSInteger sources = [[NSTimeZone localTimeZone] secondsFromGMTForDate:self];
    NSTimeInterval interval = -sources;
    return [NSDate dateWithTimeInterval:interval sinceDate:self];
}

+ (NSDate *)convertLocalGMT:(NSString *)dateString {
    NSDate * date = [[self class] convertDateGMTByString:dateString];
    NSInteger seconds = [[NSTimeZone localTimeZone] secondsFromGMTForDate:date];
    NSInteger sources = [[NSTimeZone timeZoneWithAbbreviation:@"UTC"] secondsFromGMTForDate:date];
    NSTimeInterval interval = seconds-sources;
    return [NSDate dateWithTimeInterval:interval sinceDate:date];
}

+ (NSDate *)convertDateGMTByString:(NSString *)dateString {
    if (dateString) {
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
        NSString * format = @"yyyy-MM-dd'T'HH:mm:ssZ";
        [dateFormatter setDateFormat:format];
        NSDate * date = [dateFormatter dateFromString:dateString];
        return date;
    }
    return nil;
}

+ (NSString *)convertGMTDateToString:(NSDate *)date {
    if (date) {
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
        return [dateFormatter stringFromDate:date];
    }
    return @"";
}

- (NSDate*)convertDateWithTimeZone:(NSString *)timeZoneName {
    if (timeZoneName.length) {
        NSInteger interval = [[NSTimeZone timeZoneWithName:timeZoneName] secondsFromGMT];
        interval = interval - [[NSTimeZone localTimeZone] secondsFromGMT];
        return [NSDate dateWithTimeInterval:interval sinceDate:self];
    } else {
        return self;
    }
}

- (NSDate*)convertDateBackLocalWithTimeZone:(NSString*)timeZoneName {
    if (timeZoneName.length) {
        NSInteger interval = [[NSTimeZone timeZoneWithName:timeZoneName] secondsFromGMT];
        interval = [[NSTimeZone localTimeZone] secondsFromGMT]-interval;
        return [NSDate dateWithTimeInterval:interval sinceDate:self];
    } else {
        return self;
    }
}

- (NSString*)convertToStringWithTimeZone:(NSString *)timeZoneName {
    if (timeZoneName.length) {
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:timeZoneName]];
        return [dateFormatter stringFromDate:self];
    } else {
        return [NSDate convertGMTDateToString:self];
    }
}

- (NSString*)convertToDescriptionWithTimeZone:(NSString *)timeZoneName {
    if (timeZoneName.length) {
        return [NSDate convertDateToDescription:[self convertDateWithTimeZone:timeZoneName]];
    } else {
        return [NSDate convertDateToDescription:self];
    }
}

@end
































